package com.insurance.service.impl;

import java.util.List;
import javax.annotation.Resource;
import org.hibernate.SessionFactory;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.insurance.bean.Caze;
import com.insurance.service.CazeService;


@Transactional
public class CazeServiceBean implements CazeService {
	@Resource private SessionFactory sessionFactory;

	public void save(Caze caze){
		sessionFactory.getCurrentSession().persist(caze);
	}
	
	public void update(Caze caze){
		sessionFactory.getCurrentSession().merge(caze);
	}
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	
	public Caze getCaze(Integer cazeid){
		return (Caze)sessionFactory.getCurrentSession().get(Caze.class, cazeid);
	}

	public void delete(Integer cazeid){
		sessionFactory.getCurrentSession().delete(
				sessionFactory.getCurrentSession().load(Caze.class, cazeid));
	}
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	@SuppressWarnings("unchecked")
	public List<Caze> getCazes(){		
		return sessionFactory.getCurrentSession().createQuery("from Caze").list();
	}
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	@SuppressWarnings("unchecked")
	public List<Caze> getCazesByWriter(String userName){		
		return sessionFactory.getCurrentSession().
		createQuery("from Caze c where c.userName=" + "userName").list();
	}

	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	@SuppressWarnings("unchecked")
	public List<Caze> getCazesByType(int cazeType){
		return sessionFactory.getCurrentSession().createQuery("from Caze c where c.cazeType="+cazeType).setMaxResults(5).list();
	}
	
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	@SuppressWarnings("unchecked")
	public List<Caze> getCazesTop10(){		
		return sessionFactory.getCurrentSession().createQuery("from Caze order by id desc").
		setFirstResult(0).setMaxResults(10).list();
	}
	
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	@SuppressWarnings("unchecked")
	public List<Caze> getCazesTop5(){		
		return sessionFactory.getCurrentSession().createQuery("from Caze order by id desc").
		setFirstResult(0).setMaxResults(5).list();
	}
}
